using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace HWEngine.Tools
{
	public class DoubleBufferGraphics
	{
		private BufferedGraphicsContext _gfxContext;
		private BufferedGraphics _gfx;

		public void InitDoubleBufferedGraphics(Control cntrl)
		{
			_gfxContext = BufferedGraphicsManager.Current;
			_gfxContext.MaximumBuffer = new Size(cntrl.Width + 1, cntrl.Height + 1);
			if (_gfx != null)
			{
				_gfx.Dispose();
				_gfx = null;
			}
			_gfx = _gfxContext.Allocate(cntrl.CreateGraphics(), new Rectangle(0, 0, cntrl.Width, cntrl.Height));
		}

		public Graphics Graphics
		{
			get { return _gfx.Graphics; }
		}

		public void Render(Graphics target)
		{
			_gfx.Render(target);
		}
	}
}